const PENDING = 1
const FULLFILLED = 2
const REJECTED = 3

class Promise {
    constructor(executor) {
        this.state = PENDING

        const resolver = value => {
            if (this.state === PENDING) {
                this.value = value
                this.state = FULLFILLED

                for (let [onFullfill, resolve] of this.fullfills) {
                    const x = onFullfill(this.value)
                    resolve(x)
                }
            }
        }

        const rejector = error => {}
        this.fullfills = []
        executor(resolver, rejector)
    }

    then(onFullfill) {
        return new Promise((resolve, reject) => {
            switch (this.state) {
                case FULLFILLED:
                    const x = onFullfill(this.value)
                    resolve(x)
                    break
                case PENDING:
                    this.fullfills.push([onFullfill, resolve])
                    break
            }
        })
    }
}

const promise = new Promise((resolve, rejcet) => {
    setTimeout(() => {
        resolve(100)
    }, 100)
})
    .then(data => {
        console.log(data)
        return new Promise(resolve => {
            resolve('abc')
        })
    })
    .then(data => console.log(data))